<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Services\MotionService;
use App\Services\CourseService;
use App\Services\CourseContentService;
use Session;

class MotionController extends Controller{
    public function list($mid){
        if(!$mid){
            $_data = array('code'=>'3x000001','msg'=>'请登录','url'=>env('APP_URL'));
        }else{
            $course = new CourseService;
//			$content = new CourseContentService;
            $motion = new MotionService;

            $motionlist = $motion->getMotionList($mid);
            if($motionlist === false){
                $_data = array('code'=>'4x000001','msg'=>'暂无锻炼记录');
            }else{
                $list = array();
                foreach($motionlist as $mtm=>$mst){
                    $list[$mst['curriculum_id']][] = $mst;
                }
                foreach($list as $itm=>$lst){

                    $list[$itm]['last_date'] = $lst[0]['watch_date'];
                    $list[$itm]['motion_count'] = count($lst);
                    $list[$itm]['course_info'] = $course->getCourseInfoById($itm);
                }
                $_data = array('code'=>'2x000001','motionlist'=>$list);
            }
        }

        return response()->json($_data);
    }

    public function store(){

        if(!intval($_POST['member_id'])){
            $_data = array('code'=>'3x000001','msg'=>'请登录','url'=>env('APP_URL'));
        }else{
            $motion = new MotionService;
            $data['curriculum_id'] = intval($_POST['curriculum_id']);
            $data['curriculum_content_id'] = intval($_POST['curriculum_content_id']);
            $data['member_id'] = intval($_POST['member_id']);
            $data['watch_date'] = date('Y-m-d H:i:s');

            $info = $motion->getMotionInfo(intval($_POST['member_id']),$data['curriculum_id'],$data['curriculum_content_id']);
            if(!empty($info)){
                $_data = array('code'=>'3x000002','msg'=>'该视频已锻炼');
            }else{
                $res = $motion->insertMotion($data);
                if($res){
                    $_data = array('code'=>'2x000001','msg'=>'锻炼记录添加成功');
                }else{
                    $_data = array('code'=>'4x000001','msg'=>'锻炼记录添加失败');
                }
            }

            return response()->json($_data);
        }
    }
}